package Demo01;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—06—13
 * Time: 14:12
 */
class MyArray<T> {
    public T[] array;

    public MyArray(Class<T> clazz, int capacity) {
        array = (T[]) Array.newInstance(clazz,capacity);
    }
    public T[] getArray() {
        return array;
    }
    public void setVal(int pos,T val) {
        this.array[pos] = val;
    }
    public T getPos(int pos) {
        return this.array[pos];
    }
}
class MyArray2<T> {
    public T[] array = (T[])new Object[10];
    //public T[] array = new T[10];//-->public Object[] array = new Object[10];-->会存在一个object数组

    public T[] getArray() {
        return array;
    }
    public void setVal(int pos,T val) {
        this.array[pos] = val;
    }
    public T setPos(int pos) {
        return this.array[pos];
    }
}
public class Test01 {
    public static void main(String[] args) {//指定数组类型是Integer
        MyArray<Integer> myArray = new MyArray<>(Integer.class,10);
        myArray.setVal(0,10);
        Integer[] tmp = myArray.getArray();
        System.out.println(Arrays.toString(tmp));
    }

/*    public static void main2(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        myArray.setVal(0,10);
        Integer[] tmp = myArray.getArray();
        System.out.println(Arrays.toString(tmp));
    }*/
}
